package edu.ec.java02.group02.controller;

import edu.ec.java02.group02.domain.Order;
import edu.ec.java02.group02.domain.OrderDetail;
import edu.ec.java02.group02.domain.ProductInfo;
import edu.ec.java02.group02.service.ManagerService;
import edu.ec.java02.group02.util.Page;
import edu.ec.java02.group02.util.ResultMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    ManagerService managerService;

    /**
     * 进入订单列表
     * @param request
     * @return
     */
    @RequestMapping("/orderList")
    public String orderList(HttpServletRequest request){
        return "/order/order-list";
    }

    @RequestMapping("/getOrderPage")
    @ResponseBody
    public ResultMap<List<Order>> getOrderPage(Page page, int limit,HttpServletRequest request){
        page.setRows(limit);
        page.setOrderNo(request.getParameter("orderNo"));
        page.setOrderStatus(request.getParameter("orderState"));
        List<Order> orderList = managerService.selectOrderListPage(page);
        List<Order> list = managerService.getOrderList();
        page.setTotalRecord(list.size());
        return new ResultMap<List<Order>>("",orderList,0,list.size());
    }

    /**
     * 进入详细订单列表
     * @param request
     * @return
     */
    @RequestMapping("/orderDetailList")
    public String orderDetailList(HttpServletRequest request){
        return "/order/order-detaillist";
    }

    /**
     * 查询订单详细信息-分页
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/getOrderDetailListPage")
    @ResponseBody
    public ResultMap<List<OrderDetail>> getOrderDetailListPage(Page page,int limit,HttpServletRequest request){
        page.setRows(limit);
        page.setOrderNo(request.getParameter("orderNo"));
        page.setProductName(request.getParameter("productName"));
        page.setUserId(request.getParameter("userId"));
        page.setProductId(request.getParameter("productId"));
        List<OrderDetail> orderDetailList = managerService.selectOrderDetailListPage(page);
        int totals = managerService.selectOrderDetailPageCount(page);
        page.setTotalRecord(totals);
        return new ResultMap<List<OrderDetail>>("",orderDetailList,0,totals);
    }

    /**
     * 进入添加订单界面
     * @return
     */
    @RequestMapping("addOrderHtm")
    public String addOrderHtm(){
        return "/order/order-add";
    }

    /**
     * 添加订单信息（含详细信息）
     * @return
     */
    @RequestMapping("/addOrder")
    //@ResponseBody
    public String addOrder(HttpServletRequest request){
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setProductId(Integer.parseInt(request.getParameter("productId")));
        orderDetail.setUserId(Integer.parseInt(request.getParameter("userId")));
        orderDetail.setProductNumber(Integer.parseInt(request.getParameter("productNumber")));
        managerService.saveOrderInfo(orderDetail);
        return "/order/order-add";
    }

    /**
     * 进入更新订单详细信息界面
     * @param orderDetailId
     * @param request
     * @return
     */
    @RequestMapping("/updateOrderDetailHtm")
    public String updateOrderDetailHtm(String orderDetailId,HttpServletRequest request){
        OrderDetail orderDetail = managerService.getOrderDetailByOrderDetailId(Integer.parseInt(orderDetailId));
        request.setAttribute("Order",orderDetail);
        return "/order/order-edit";
    }

    /**
     * 修改订单-详细信息
     * @return
     */
    @RequestMapping("/updateOrderDetail")
    @ResponseBody
    public String updateOrderDetail(HttpServletRequest request){
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setOrderDetailId(Integer.parseInt(request.getParameter("orderDetailId")));
        orderDetail.setProductId(Integer.parseInt(request.getParameter("productId")));
        orderDetail.setProductName(request.getParameter("productName"));
        orderDetail.setUserId(Integer.parseInt(request.getParameter("userId")));
        orderDetail.setProductNumber(Integer.parseInt(request.getParameter("productNumber")));
        orderDetail.setProductTotalprice(Double.parseDouble(request.getParameter("productTotalprice")));
        managerService.modifyOrderDetailInfo(orderDetail);
        return "/order/order-edit";
    }

    /**
     * 根据Id将订单详细信息导出Excel
     * @param orderDetailId
     * @return
     */
//    @RequestMapping("/excelOrder")
//    @ResponseBody
//    public String excelOrder(String orderDetailId,HttpServletRequest request){
//        return managerService.excelOrderInfo(orderDetailId,request.getServletContext().getRealPath("/excel/")+orderDetailId+".xlsx");
//    }

    /**
     *根据Id删除订单详细信息
     * @param orderIds
     * @param orderNos
     * @return
     */
    @RequestMapping("/delOrder")
    @ResponseBody
    public String delOrder(String orderIds,String orderNos){
        return managerService.deleteOrder(orderIds,orderNos);
    }

}
